package unionfindset;


/**
 * @ClassName UnionFind
 * @Description 左程云 并查集   小挂大  扁平化
 * @Author ZJX
 * @Date 2024/10/23 20:55
 * @Version 1.0
 */
class UnionFind {
    public int[] parent;
    public int[] size;



    public UnionFind(int n) {
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            size[i] = 1;
        }
    }

    public int find(int x) {
        if (parent[x]!= x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    public boolean isSameSet(int x,int y){
        return find(x) == find(y);
    }

    public void union(int x, int y) {
        parent[find(x)] = find(y);
        return;
    }
}
